<?php

class WebAdmin extends CWebUser {

    public $loginUrl = array('admin/main/login');
    public $allowAutoLogin = true;
    private static
            $successName = 'successMsg',
            $noticeName = 'noticeMsg',
            $errorName = 'errorMsg';

    public function pushMessage($string = null, $type = 'successMsg') {
        if ($string != null) {
            if ($this->hasFlash($type))
                $old = $this->getFlash($type);
            else
                $old = array();
            array_push($old, $string);
            $this->setFlash($type, $old);
        }
    }

    public function showPutMsg() {
        $show = '';
        if ($this->hasFlash(self::$successName)) {
            $show.= '<div class="flash-success">';
            $msg = (array) $this->getFlash(self::$successName);
            if (count($msg) > 1) {
                $show.= mb_substr(implode(', ', $msg), 0, -1, 'utf-8');
            } else {
                $show.= current($msg);
            }
            $show.= '</div>';
        }
        if ($this->hasFlash(self::$noticeName)) {
            $show.= '<div class="flash-notice">';
            $msg = (array) $this->getFlash(self::$noticeName);
            if (count($msg) > 1) {
                $show.= mb_substr(implode(', ', $msg), 0, -1, 'utf-8');
            } else {
                $show.= current($msg);
            }
            $show.= '</div>';
        }
        if ($this->hasFlash(self::$errorName)) {
            $show.= '<div class="flash-error">';
            $msg = (array) $this->getFlash(self::$errorName);
            if (count($msg) > 1) {
                $show.= mb_substr(implode(', ', $msg), 0, -1, 'utf-8');
            } else {
                $show.= current($msg);
            }
            $show.= '</div>';
        }
        return $show;
    }

    public function putSuccessMsg($string = null) {
        $this->pushMessage($string, self::$successName);
    }

    public function putNoticeMsg($string = null) {
        $this->pushMessage($string, self::$noticeName);
    }

    public function putErrorMsg($string = null) {
        $this->pushMessage($string, self::$errorName);
    }

}